function ajax(settings) {
    var defaultSetting = {
        type: "POST",
        dataType: "json",
        success: function (data) {
        },
        beforeSend: function (XMLHttpRequest) {
        },
        complete: function (XMLHttpRequest, textStatus) {
        }
    };

    $.extend(defaultSetting, settings);

    $.ajax({
        type: defaultSetting.type,
        url: defaultSetting.url,
        data: defaultSetting.data,
        success: function (data) {
            if (data.error != null) {
                alert(data.error);
            } else {
                defaultSetting.success(data);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (defaultSetting.error) {
                defaultSetting.error(XMLHttpRequest, textStatus, errorThrown);
            }
            else {
                console.log(XMLHttpRequest);
                alert('系统调用出错，错误编号:' + XMLHttpRequest.status);
            }
        },
        beforeSend: function (XMLHttpRequest) {
            defaultSetting.beforeSend(XMLHttpRequest);
        },
        complete: function (XMLHttpRequest, textStatus) {
            defaultSetting.complete(XMLHttpRequest, textStatus);
        },
        dataType: defaultSetting.dataType
    });
}

/*初始化dataTable控件*/
function createDatatable(id, settings) {
    var defSettings = {
        "dom": 'tr<"bottom"lpi><"clear">',
        "bRetrieve": true,
        "processing": true,
        "order": [],
        language: {
            "lengthMenu": "每页 _MENU_ 条记录 ",
            "zeroRecords": "没有找到记录",
            "info": "&nbsp;&nbsp;第 _PAGE_ 页 ( 总共 _PAGES_ 页, 共 _TOTAL_ 项)",
            "infoEmpty": "无记录",
            "loadingRecords": "正在加载数据...",
            "processing": "请等待...",
            "infoFiltered": "(从 _MAX_ 条记录过滤)",
            "search": "搜索:",
            paginate: {
                first: '首页',
                previous: '上一页',
                next: '下一页',
                last: '尾页'
            },
            aria: {
                paginate: {
                    first: 'First',
                    previous: 'Previous',
                    next: 'Next',
                    last: 'Last'
                }
            }
        }
    };
    $.extend(defSettings, settings);
    var table = $(id).DataTable(defSettings);

    //checkbox全选
    $(id + " #checkAll").on("click", function () {
        if ($(this).prop("checked") === true) {
            $(id + " input[name='checkList']").prop("checked", $(this).prop("checked"));
            $(id + ' tbody tr').addClass('selected');
        } else {
            $(id + " input[name='checkList']").prop("checked", false);
            $(id + ' tbody tr').removeClass('selected');
        }
    });
    //checkbox不选中行
    $(id + ' tbody').on('click', 'tr input[name="checkList"]', function () {
        var $tr = $(this).parents('tr');
        $tr.toggleClass('selected');
        var $tmp = $(id + ' [name=checkList]:checkbox');
        $(id + ' #checkAll').prop("checked", $tmp.length == $tmp.filter(':checked').length);
        $(id + ' #checkAll').parents('span').toggleClass('checked', $tmp.length == $tmp.filter(':checked').length);
    });

    return table;
}

window.alert = function (title, msg, callback) {
    var dialogHTML = '<div id="selfAlert" class="modal fade">';
    dialogHTML += '<div class="modal-dialog">';
    dialogHTML += '<div class="modal-content">';
    dialogHTML += '<div class="modal-header">';
    dialogHTML += '<button type="button" class="close" data-dismiss="modal" aria-label="Close">';
    dialogHTML += '<span aria-hidden="true">&times;</span>';
    dialogHTML += '</button>';
    dialogHTML += '<h4 class="modal-title">提醒</h4>';
    dialogHTML += '</div>';
    dialogHTML += '<div class="modal-body">';
    dialogHTML += title;
    dialogHTML += '</div>';
    dialogHTML += '<div class="modal-footer">';
    dialogHTML += '<button type="button" class="btn btn-primary" data-dismiss="modal">确定</button>';
    dialogHTML += '</div>';
    dialogHTML += '</div>';
    dialogHTML += '</div>';
    dialogHTML += '</div>';

    if ($('#selfAlert').length <= 0) {
        $('body').append(dialogHTML);
    }

    $('#selfAlert').on('hidden.bs.modal', function () {
        $('#selfAlert').remove();
        if (typeof callback == 'function') {
            callback();
        }
    }).modal('show');
}

function initLoading() {
    $("body").append("<!-- loading -->" +
        "<div class='modal fade' id='loading' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' data-backdrop='static'>" +
        "<div class='modal-dialog' role='document'>" +
        "<div class='modal-content'>" +
        "<div class='modal-header'>" +
        "<h4 class='modal-title' id='myModalLabel'>提示</h4>" +
        "</div>" +
        "<div id='loadingText' class='modal-body'>" +
        "<span class='glyphicon glyphicon-refresh' aria-hidden='true'>1</span>" +
        "处理中，请稍候。。。" +
        "</div>" +
        "</div>" +
        "</div>" +
        "</div>"
    );
}